/**
 * 报表上报公共方法
 */
import { httpAction, getAction, postAction,deleteAction} from '@/api/manage'
import pubMothod from './PublicMethod'
import store from '@/store/'

export const BbsbModelMixin1 = {
  data() {
    return {
      userInfo: store.getters.userInfo,
      departInfo: store.getters.departInfo,
      Loading: false,
      disableSubmit: false,
      dataSource: [],
      title: '',
      visible: false,
      isCheck: false
    }
  },
  mounted() {},
  computed: {},
  methods: {
    add(record) {
      this.Loading = true
      this.model = {}
      this.model.year = record.year
      this.model.quarter = record.quarter
      this.visible = true
      this.creatReport()
      this.setReportTitle()
    },
    check(record) {
      this.Loading = true
      this.model = Object.assign({}, record)
      this.visible = true
      this.getTableData()
    },
    edit(record) {
      this.Loading = true
      this.model = Object.assign({}, record)
      setTimeout(() => {
        this.getTableData()
      }, 2000)
      this.visible = true
    },
    delete(record) {
      deleteAction(this.url.delete, { id: record.id }).then(res => {
        if (res.success) {
          this.$message.success(res.message)
          this.$emit('ok')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 获取报表表格信息
    getTableData() {
      getAction(this.url.list, { id: this.model.id, type: this.type }).then(res => {
        if (res.success && res.result.length > 0) {
          this.dataSource = JSON.parse(res.result[0].reportInfo)
          this.model.createTime = res.result[0].createTime
          this.model.createByText = res.result[0].createByText
          this.model.auditPersonName = res.result[0].auditPersonName
          this.model.departName = res.result[0].departName
          this.model.year = res.result[0].year
          this.model.quarter = res.result[0].quarter
          this.model.fileId = res.result[0].fileId
        }
        this.Loading = false
      })
    },
    // 关闭报表
    close(isSubmit) {
      if (isSubmit) {
        // 弹出选择审核人
        this.$refs.approver.showApproveConfirm = true
      } else {
        this.visible = false
      }
    },
    // 数据初始化
    creatReport() {
      getAction(this.url.getCreatData, {
        type: this.type,
        ...this.model
      }).then(res => {
        if (res.success && res.result[0]) {
          this.dataSource = JSON.parse(res.result[0].reportInfo)
          this.Loading = false
        } else {
        }
      })
    },
    // 保存数据
    saveData(id) {
      this.Loading = true
      let httpurl, method, param
      const string = JSON.stringify(this.dataSource)
      if (!this.model.id) {
        httpurl = this.url.add
        method = 'post'
        param = {
          fileId: id,
          reportInfo: string,
          year: this.model.year,
          quarter: this.model.quarter,
          type: this.type,
          shenRespId: this.model.shenRespId,
          createBy: this.model.createBy,
          addtime: this.model.addtime,
          officeId: this.model.officeId
        }
      } else {
        httpurl = this.url.edit
        method = 'put'
        param = {
          id: this.model.id,
          type: this.type,
          fileId: id,
          reportInfo: string
        }
      }
      httpAction(httpurl, param, method).then(res => {
        this.Loading = false
        if (res.success) {
          if (!this.model.id) {
            this.model.id = res.result
          }
          if (this.btnStyle == 'save') {
            this.$message.success(res.message)
            this.visible = false
            this.$emit('ok')
            return
          } else {
            this.submitData(this.approveId)
          }
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 提交数据
    submitData(userId) {
      getAction(this.url.submitData, {
        proId: this.model.id,
        userIds: userId,
        type: this.type,
        state: this.state ? this.state : undefined
      }).then(res => {
        this.Loading = false
        if (res.success) {
          this.$message.success(res.message)
          this.visible = false
          this.$refs.approver.confirmLoading = false
          this.$emit('ok')
        } else {
          console.log(res)
        }
      })
    },
    // 获取报表头部信息
    setReportTitle() {
      console.log(this.departInfo);
      const nowDate = new Date()
      this.model.createTime = pubMothod.dateFormat('yyyy-MM-dd hh:mm:ss', nowDate)
      this.model.addtime = parseInt(Number(nowDate.getTime()) / 1000)
      this.model.createByText = this.userInfo.realname
      this.model.auditPersonName = this.userInfo.realname
      this.model.departName = this.departInfo.departName
      this.model.detailName = this.departInfo.detailName
      this.model.createBy = this.userInfo.id
      this.model.shenRespId = this.userInfo.id
      this.model.officeId = this.departInfo.id
    },
    // 获取审批日志信息
    checkAuditResult(){
      getAction(this.url.auditResult,{auditInfoId:this.model.id}).then(res=>{
         
      })
    }
  }
}
